//ViewsSelect.java
import javax.swing.event.*;
import javax.swing.*;
// glazed lists
import ca.odell.glazedlists.*;
import ca.odell.glazedlists.matchers.*;
import ca.odell.glazedlists.swing.*;
import java.util.*;

/**
 * This {@link MatcherEditor} matches issues if their user is selected.
 *
 */
public class ViewsSelect extends AbstractMatcherEditor implements ListSelectionListener {

   /** a list of users */
   EventList viewsEventList;
   EventList viewsNameList;
   EventList viewsSelectedList;
   EventList actualViews = new BasicEventList();
   LinkedList listViewObjects = new LinkedList();

   /** a widget for selecting users */
   JList viewsJList;

   /**
    * Create a {@link IssuesForUsersMatcherEditor} that matches users from the
    * specified {@link EventList} of {@link Issue}s.
    */
   public ViewsSelect(EventList viewEventList,EventList viewNameList) {

       this.viewsEventList = viewEventList;
       this.viewsNameList = viewNameList;

       // create a JList that contains users
       EventListModel viewsListModel = new EventListModel(viewsNameList);
       viewsJList = new JList(viewsListModel);

       // create an EventList containing the JList's selection
       EventSelectionModel viewSelectionModel = new EventSelectionModel(viewsNameList);
       viewsJList.setSelectionModel(viewSelectionModel);
       viewsSelectedList = viewSelectionModel.getSelected();

       // handle changes to the list's selection
       viewsJList.addListSelectionListener(this);
   }


   /**
    * Get the widget for selecting users.
    */
   public JList getJList() {
       return viewsJList;
   }



public EventList getEventList(){
return viewsEventList;
}

public boolean addViewToJList(String value){
boolean temp = viewsEventList.add(value);
return temp;
}


public void addViewNameList(String viewName){
   viewsNameList.add(viewName);
}

public void addViewEventList(Views view){
   viewsEventList.add(view);
}

public void addView(String viewName, Views view){
   addViewNameList(viewName);
   addViewEventList(view);

}

public void deleteView(){
  LinkedList viewsList = getSelectedViews();
  Iterator viewListIterator = viewsList.listIterator();
  System.out.println(viewsList.size());
  while(viewListIterator.hasNext()){
     Views temp = (Views)viewListIterator.next();
     viewListIterator.remove();
     String tempName = temp.getViewName();
     System.out.println(tempName);
     viewsEventList.getReadWriteLock().writeLock().lock();
     viewsEventList.remove(temp);
     viewsEventList.getReadWriteLock().writeLock().unlock();
     viewsNameList.getReadWriteLock().writeLock().lock();
     viewsNameList.remove(tempName);
     viewsNameList.getReadWriteLock().writeLock().unlock();
  }
}

public LinkedList getSelectedViews(){
   Iterator viewNameIterator = getIterator();
   while(viewNameIterator.hasNext()){
      String name = (String)viewNameIterator.next();
      listViewObjects.add(nameToView(name));
   }
listViewObjects.size();
   return listViewObjects;
}


public Views nameToView(String name) {

   Views temp = new Views("NULL");
   Iterator viewObjectIterator = viewsEventList.listIterator();
   while(viewObjectIterator.hasNext()){
      temp = (Views)viewObjectIterator.next();
      if (temp.getViewName().equals(name)){
         return temp;
      }
   }
   return temp;
}

 public boolean alreadyExists(String name){
    if(viewsNameList.contains(name)){
       return true;
    }
    else{
       return false;
    }
 }

 public boolean deleteViewFromJList(String value){
   boolean temp = viewsEventList.remove(value);
   return temp;
 }

 public Iterator getIterator(){
   return viewsSelectedList.iterator();
 }

  public void valueChanged() {
    Matcher newMatcher = new ViewsToFilesMatcher(getSelectedViews());
    fireChanged(newMatcher);
    listViewObjects.removeAll(listViewObjects);
  }

  /**
      * When the JList selection changes, create a new Matcher and fire
      * an event.
      */
     public void valueChanged(ListSelectionEvent e) {
         Matcher newMatcher = new ViewsToFilesMatcher(getSelectedViews());
         fireChanged(newMatcher);
         listViewObjects.removeAll(listViewObjects);
   }
}
